// packageK/merchants/merchants.js
import drawQrcode from '../lib/weapp.qrcode.min'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    span: 12,
    QRcodeText: 'http://ww.baidu.com/',
    total_business: '',
    current_month_total_business: '',
    total_already_business: '',
    withdraw_able_amount: '-',
    career_type: null,
    mc_merchants_id: null
  },

  /**
   * 生成二维码
   */
  generateQRcode: function() {
    drawQrcode({
      width: 200,
      height: 200,
      canvasId: "myQrcode",
      text: this.data.QRcodeText
    })
  },

  /**
   * 下载二维码
   */
  downloadQRcode: function() {
    wx.showLoading({
      title: '加载中...',
    })
    wx.canvasToTempFilePath({
      canvasId: "myQrcode",
      success: function(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function() {
            app.tips('保存成功')
          }
        })
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 获取统计数据
   */
  getStatisticalData: function() {
    const _this = this
    const url = app.getNetAddresss('platformApplet.statistics.get-merchants')
    wx.showLoading({
      title: '加载中...',
    })
    app._getNetWork({
      url,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const { total_business, current_month_total_business, total_already_business, withdraw_able_amount, career_type, mc_merchants_id } = resData.data
          _this.setData({
            total_business: total_business + '个',
            current_month_total_business: current_month_total_business + '个',
            total_already_business: total_already_business + '个',
            withdraw_able_amount: '¥' + withdraw_able_amount,
            career_type,
            mc_merchants_id
          })
        } else {
          app.tips(resData.msg)
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getStatisticalData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.generateQRcode()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})